package tk.mybatis.springboot.common.base;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import tk.mybatis.springboot.common.Page;
import tk.mybatis.springboot.common.message.ResponseData;
import tk.mybatis.springboot.common.util.StringUtil;
import tk.mybatis.springboot.conf.ResourceProperties;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by jie_r on 2016/11/27.
 */
public class BaseController {
    public static final int SUCCESS = 0;
    public static final int FAILED = 1;
    public static final int FAULT = -1;

    protected static String PROJECT_NAME;
    protected static String PROJECT_PATH;
    protected static String PROJECT_ADMIN_INDEX;
    protected static String PROJECT_ADMIN_LOGIN;

    @Resource
    protected ResourceProperties resource;

    // 当前会话
    protected HttpSession session;

    protected HttpServletRequest request;

    // 设置页数
    protected Page page;

    // 图片服务器地址
    protected String picIPAddress;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request) throws Exception {
        this.request = request;
        this.session = request.getSession();

        ServletContext application = request.getSession().getServletContext();
        Integer pageSize = (Integer) application.getAttribute("pageSize");
        this.setPage(request.getParameter("curPage"),
                StringUtil.isEmpty(request.getParameter("pageSize")) ? pageSize + "" : request.getParameter("pageSize"));
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    /**
     * 设置page对象
     */
    private void setPage(String curPage, String pageSize) {
        this.page = new Page(curPage, pageSize);
    }

    public ResponseData toError(String message) throws Exception {
        return new ResponseData(FAILED, message);
    }

    public ResponseData toSuccess(String message) throws Exception {
        return new ResponseData(SUCCESS, message);
    }

    public ResponseData toSuccess(String message, Object data) throws Exception {
        return new ResponseData(SUCCESS, message, data);
    }


    /**
     * 为图片添加图片服务器
     *
     * @param picture
     * @return
     */
    protected String resetPicAddress(String picture) {
        if (StringUtil.isEmpty(picture)) return null;
        return picIPAddress + picture;
    }

    /**
     * 下载
     *
     * @param bytes
     * @param fileName
     * @param res
     * @throws IOException
     */
    public void export(byte[] bytes, String fileName, HttpServletResponse res) throws IOException {
        if (bytes.length > 0) {
            res.reset();
            res.setContentType("application/octet-stream");
            res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            res.setContentLength(bytes.length);
            OutputStream outputStream = new BufferedOutputStream(res.getOutputStream());
            try {
                outputStream.write(bytes);
            } catch (IOException ex) {
                System.out.println("异常了");
            }
        } else {
            res.setCharacterEncoding("UTF-8");
            res.setContentType("application/json;charset=utf-8");
            res.setStatus(200);
            PrintWriter printWriter = res.getWriter();
            printWriter.println("{\"code\": -1, \"message\": \"没有相关数据！\"}");
            printWriter.close();
        }

    }
}
